<template>
	<view class="container">
		<view class="wrapper">
			<view class="main" >
				<scroll-view class="scroll" :scroll-y="true" :scroll-top="scrollTop" :show-scrollbar="false"
					:refresher-threshold="50" :refresher-triggered="refreshStatus" :lower-threshold="100"
					:scroll-with-animation="true" refresher-enabled="true" @refresherrefresh="onRefreshData"
					@scrolltolower="onLoadData" @scroll="onScroll">
					
					<view class="section" v-for="(item, index) in list" :key="index">
						<view class="card" style="align-items: center;justify-items: center;" @click="onLink('/pagesWorkbenches/pages/healthGuide/detail/detail?id='+item.id)">
							<u-avatar size="200" shape="square" :src="item.img[0]"></u-avatar>
							<view class="content" style="justify-items: center;align-items: center;">
								<view class="pdleft">
									<view class="title" style="min-height: 80rpx;overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical;">
										{{ item.schemeTitle }}
									</view>
									<view  class="desc" style="line-height: 40rpx;">
										浏览量：{{ item.browse }}
									</view>
									<view  class="desc" style="line-height: 40rpx;">
										评分：{{ item.rating }}
									</view>
									<view  class="desc" style="line-height: 40rpx;">
										发布时间：{{ item.createTime.substr(0,10) }}
									</view>
								</view>
							</view>
						</view>
						<view style="border-top: 1px solid #e5e5e5;padding: 20rpx 10rpx 0rpx 10rpx;">
							<u-grid :border="true" :col="2">
								<u-grid-item >
									<text style="font-size: 31rpx;" @click="delGuide(item.id)">删除</text>
								</u-grid-item >
								<u-grid-item >
									<text style="font-size: 31rpx;" @click="editGuide(item.id)">编辑</text>
								</u-grid-item >
							</u-grid>
						</view>
					</view>
					
					<view class="nullData" v-if="page == 1 && list.length <=0">
						<view>
							<view>
								<image src="../../../static/image/notdata.png" class="nullImage"></image>
							</view>
							<view class="nullText">
								暂无记录
							</view>
						</view>
					</view>
				</scroll-view>
			</view>
		</view>
		<view style="height: 70px;background-color: #ffffff;position: fixed;bottom: 0px;width: 100%; ">
			<view style="padding:30rpx">
				<u-button text="新增健康指南" @click="onLink('/pagesWorkbenches/pages/healthGuide/mainten/mainten')" color="#155CCF"></u-button>
			</view>
		</view>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				//当前页数
				page: 1,
				//每页显示条数
				size: 10,
				//列表数据
				list: [],
				//总页数
				pages: 0,
				//监听滚动
				scrollTop: 0,
				realScrollTop: 0,
				refreshStatus: false
			}
		},
		onLoad() {
			
		},
		onShow() {
			this.fetchList();
		},
		methods: {
			// 健康指南
			fetchList() {
				uni.showLoading({
					title: "加载中",
					mask: true
				})
				var params = {};
				params.current = this.page;
				params.size = this.size;
				params.userId = uni.getStorageSync('userId');
				this.$request({
					url: '/blade-merchant-app/merchant-app/healthGuide/list',
					data: params
				}).then(res => {
					const {
						records = [],
							pages
					} = res.data;
					
					records.forEach(item => {
						item.img = item.img.split(',');
					})
					
					if (this.page === 1) {
						this.list = records;
					} else {
						this.list = this.list.concat(records);
					}
					this.pages = pages || 0;
					setTimeout(() => {
						this.refreshStatus = false;
						uni.hideLoading();
					}, 300);
				})
			},
			// 监听滚动事件
			onScroll(e) {
				this.realScrollTop = e.detail.scrollTop;
			},
			// 刷新数据
			onRefreshData() {
				this.refreshStatus = true;
				this.page = 1;
				this.list = [];
				this.fetchList();
			},
			//删除场地
			delGuide(id) {
				var that = this;
				uni.showModal({
				   title: '知识库删除操作',
				   content: '是否确认删除此健康指南，删除后不可恢复？',
				   showCancel: true,
				   success: function (res) {
					   if (res.confirm) {
							uni.showLoading({
								title: '删除中',
								mask: true
							})
							that.$request({
								url: `/blade-merchant-app/merchant-app/healthGuide/remove?ids=${id}`,
								type: 'POST',
								data: {
									ids: id
								}
							}).then(res => {
								setTimeout(()=>{
									uni.hideLoading();
									that.fetchList();
								},800)
							}).catch(err =>{
								uni.hideLoading();
							})
					   }
				   }
				});
			},
			//编辑知识内容
			editGuide(id){
				this.onLink('/pagesWorkbenches/pages/healthGuide/mainten/mainten?id='+id)
			},
			// 加载数据
			onLoadData() {
				if (this.page < this.pages) {
					this.page += 1;
					this.fetchList();
				}
			},
			onLink(path) {
				uni.navigateTo({
					url: path
				})
			},
		}
	}
</script>


<style lang="scss">
	page {
		height: 100%;
		overflow: hidden;
		background: #f1f1f1;

		.container {
			height: 100%;

			.wrapper {
				display: flex;
				flex-direction: column;
				// height: calc(100% - 70px);
				height: 100%;
				
				.header {
					display: flex;
					flex-direction: row;
					padding: 52rpx 0;
					color: #fff;
					background-color: #22A5FF;
					z-index: 1;

					.subsection {
						background: #ffffff; padding: 10rpx 10rpx;
					}
				}

				.main {
					flex: 1;
					height: 100%;

					.scroll {
						height: 100%;

						.section {
							padding: 30rpx 30rpx 30rpx 30rpx;
							background-color: #ffffff;
							margin-bottom: 30rpx;

							.card {
								display: inline-flex;
								
								.imgs {
									width: 250rpx;height: 250rpx;
								}
								.content {
									width: 100%;
									
									.pdleft {
										padding-left:30rpx;
										.title {
											font-size: 33rpx;font-weight: 600;
										}
										.desc {
											font-size: 26rpx;color: #666666;line-height: 50rpx;
										}
									}
									
									
								}
								
							}

							
						}
					}
				}
			}
		}
		
		.nullData {
			height: 100%;
			display: flex;
			-webkit-box-pack: center;
			justify-content: center;
			-webkit-box-align: center;
			align-items: center;
			
			.nullImage {
				width: 120px;height: 120px;
			}
			.nullText {
				color:#666666;font-size:29rpx;padding-top:15rpx;text-align: center;
			}
			
		}
	}
	
</style>
